<?php

namespace app\admin\server;


class Upload
{
    protected static $path = [
        'absolute' => ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads',
        'relative' => DS . 'static' . DS . 'uploads',
    ];

    protected static $conf = [
        'size' => 1024 * 1024 * 10,
        'ext' => 'jpg,jpeg,jpq,png,gif',
    ];

    public static function uploads($name, $conf = [])
    {
        $file = request()->file($name);
        if (empty($file)) throw new \Exception('获取不到上传信息');

        !is_dir(self::$path['absolute']) && mkdir(self::$path['absolute'], 0755, true);

        $info = $file->validate(array_merge(self::$conf, $conf))->move(self::$path['absolute']);

        if (!$info) throw new \Exception($file->getError());

        return self::$path['relative'] . DS . $info->getSaveName();
    }
}